//
//  CitysViewController.m
//  OrderSeat
//
//  Created by 王威 on 14-1-10.
//  Copyright (c) 2014年 wangwei. All rights reserved.
//

#import "CitysViewController.h"
#import "MoreViewController.h"

@implementation CitysViewController

- (id)init
{
    self = [super initWithNibClass:[CitysViewController class]];
    if (self) {
        _dataSource = [[CitysDataSource alloc] initWithVC:self];
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
    
    [_dataSource fetchCitys];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[AppDelegate sharedInstance] hideTabBar:self.tabBarController];
}

- (void)setupUI
{
    [self createLeftButton];
    [self setNavigationTitle:@"城市切换"];
    
    _cityTable.dataSource = _dataSource;
    _cityTable.delegate = _dataSource;
}

- (void)leftButtonClick:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
    [[AppDelegate sharedInstance] showTabBar:self.tabBarController];
}


@end

//==============================================================================
#pragma=========数据源实现========================================================
//==============================================================================

#define LOCATION_CITY_TAG 1001
#define FETCH_CITYS_TAG 1002

@implementation CitysDataSource

- (id)initWithVC:(id)viewController
{
    self = [super init];
    if (self) {
        self.viewController = viewController;
        self.cityDictionary = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)locationCity
{
    NSString *url = [NSString stringWithFormat:API_LOCATION_CITY,[AppSetting longitude],[AppSetting latitude]];
    [self get:url tag:LOCATION_CITY_TAG];
}

- (void)fetchCitys
{
    [SVProgressHUD show];
    [self get:API_GET_CITY tag:FETCH_CITYS_TAG];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"%@", request.responseString);
    NSDictionary *json = [request.responseString JSONValue];
    
    if (request.tag == LOCATION_CITY_TAG) {
        NSDictionary *detail = [json objectForKey:@"detail"];
        NSArray *results = [detail objectForKey:@"results"];
        NSDictionary *data = results[0];
        NSString *city = [data objectForKey:@"c"];
        
        [_cityDictionary setObject:city forKey:@"0"];
        [_viewController.cityTable reloadData];
        
        [SVProgressHUD dismiss];
    }
    
    if (request.tag == FETCH_CITYS_TAG) {
        if (![_cityDictionary objectForKey:@"0"]) {
            [_cityDictionary setObject:@"正在获取城市..." forKey:@"0"];
        }
        
        NSArray *citys = [json objectForKey:@"city"];
        [_cityDictionary setObject:citys forKey:@"1"];
        [_viewController.cityTable reloadData];

        [self locationCity];
    }
    
    
}

#pragma ===================加载tableview数据 Begin================================================

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) return 1;
    if (section == 1) {
        NSArray *array = [_cityDictionary objectForKey:INT_TO_STR(section)];
        return array.count;
    }
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return section == 0 ? @"当前定位" : @"开通城市";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
        cell.textLabel.font = [UIFont systemFontOfSize:18];
    }
    
    if (indexPath.section == 0) {
        cell.textLabel.text = [_cityDictionary objectForKey:@"0"];
    }
    
    if (indexPath.section == 1) {
        NSArray *citys = [_cityDictionary objectForKey:@"1"];
        NSDictionary *city = [citys objectAtIndex:indexPath.row];
        cell.textLabel.text = [city objectForKey:@"name"];
    }
    
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 1) {
        NSArray *citys = [_cityDictionary objectForKey:@"1"];
        NSDictionary *city = [citys objectAtIndex:indexPath.row];
        
        NSString *cityId = [city objectForKey:@"id"];
        NSString *cityName = [city objectForKey:@"name"];
        
        [AppSetting setCityId:cityId];
        [AppSetting setCityName:cityName];
        [[BaseDataManager sharedInstance] startDownLoad2];
        
        if (_viewController.delegate) {
            [_viewController.delegate selectedDidCity:city];
        }
        
        [_viewController.navigationController popViewControllerAnimated:YES];
        [[AppDelegate sharedInstance] showTabBar:_viewController.parentViewController.tabBarController];
    }
}

@end
